package com.nexgo.payment.trans.scanquery;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.nexgo.data.entity.RecvData;
import com.nexgo.data.json.JsonRecData;
import com.nexgo.payment.action.ActionResult;
import com.nexgo.payment.action.ActionStep;
import com.nexgo.payment.aop.annotation.TransCondition;
import com.nexgo.payment.constrant.CardMode;
import com.nexgo.payment.constrant.TransType;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.trans.AbsScanTransPresenter;
import com.nexgo.payment.trans.TransFlow;
import com.nexgo.payment.util.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 扫码退货
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年9月12日
 */
@TransCondition
@TransFlow
public class ScanQueryPresenter extends AbsScanTransPresenter implements ScanQueryContract.Presenter{
    private Logger mLog = LoggerFactory.getLogger(ScanQueryPresenter.class.getSimpleName());

    private ScanQueryContract.View mView;
    private boolean mIsScanNext = false;

    public ScanQueryPresenter(@NonNull ScanQueryContract.View view, @NonNull DataSource repository) {
        super(view, repository);
        mView = checkNotNull(view);
        mView.setPresenter(this);
    }

    @Override
    protected TransData getTransData() {
        TransData transData = new TransData();
        transData.setTransType(TransType.SCAN_QUERY);
        transData.setIsAutoQuery(false);
        return transData;
    }

    @Override
    protected void onFirstAction() {
        postMainThread(() -> mView.showInputTraceCode());
    }

    @Override
    protected void onTransSuccess(RecvData recvData) {
        postMainThread(() -> mView.quitTrans());
    }

    @Override
    protected void onScanTransSuccess(JsonRecData jsonRecData) {
        postMainThread(() -> mView.onShowScanQueryMsg(jsonRecData));
    }

    @Override
    public void importActionResult(ActionResult actionResult) {
        Object data = actionResult.getData();
        ActionStep step = (ActionStep) actionResult.getAction().getActionStep();
        int ret = actionResult.getRet();
        mLog.debug("curr action step {} ret {}", step, actionResult.getRet());
        //成功处理进行处理，否则让父类处理
        if (ret == ActionResult.SUCC) {
            switch (step) {
                case INPUT_ADMIN_PSW:
                    inputAdminPasswordNext(data);
                    break;
/*                case INPUT_TRACE_CODE:
                    inputTraceCodeNext(data);
                    break;
                case INPUT_SCAN:
                    inputScanNext(data);
                    return;*/
                default:
                    break;
            }
        }
        super.importActionResult(actionResult);
    }

    /**
     * 输入主管密码后下一步
     *
     * @param data
     */
    protected void inputAdminPasswordNext(Object data) {
        postMainThread(() -> mView.showInputTraceCode());
    }

    /**
     * 输入凭证码后下一步
     *
     * @param data
     */
    protected void inputTraceCodeNext(Object data){
        String traceCode = (String) data;
        if (TextUtils.isEmpty(traceCode)){
            //跳去扫码
            postMainThread(() -> mView.showScanFragment());
            return;
        }
        //凭证码输入完毕，进入输退款金额
        mTransData.setTraceCode(traceCode);
        postMainThread(() -> mView.showOnlineProc(mTransData,null));
    }

    /**
     * 扫码后下一步动作
     *
     * @param data
     */
    public void inputScanNext(Object data) {
        mTransData.setTraceCode((String) data);
        mTransData.setCardMode(CardMode.SCAN);
        mTransData.setEntryMode(getEntryMode());
        //不从此处发起输入金额，需要等待此Activity状态恢复之后才能发起，不然会报异常
        mIsScanNext = true; //记录扫码后的状态
    }

    @Override
    protected void onShowScanOrderUrl(JsonRecData recvData) {

    }

    @Override
    protected void showScanUrlNext() {

    }


    @Override
    public void showOnScanNextAndRestart() {
        if (!mIsScanNext) return;
        postMainThread(() -> mView.showOnlineProc(mTransData, null));
        mIsScanNext = false;
    }

    @Override
    protected void inputAmountNext(Object data) {
        mTransData.setAmount(StringUtils.leftPad((String) data, 12, '0'));
        postMainThread(() -> mView.showOnlineProc(mTransData, null));
    }


    @Override
    protected void onTransQueryOrCancel(JsonRecData recvData) {
        return ;
    }

}
